#!/usr/bin/env python
# -*- coding: utf-8 -*-
#code:utf-8
#
# Copyright(C) 2011 SupDo.com
# Licensed under the GUN License, Version 3.0 (the "License");
# 
# File:          catalogView.py
# Author:      KuKei
# Create Date: 2011-11-17
# Description: 栏目管理视图
# Modify Date: 2011-11-17

from sd.cms.views.admin import AdminBaseView
from sd.cms.lib import Params, Result
from sd.cms.forms.paramForm import ColumnForm
from sd.cms.models.paramModel import Catalog, Column, Param
from sd.cms.views.admin.paramView import ParamView

class ColumnView(AdminBaseView):
    def add(self, **args):
        try:
            columnForm = ColumnForm()
            if self.request.POST:
                columnForm = ColumnForm(self)
                if columnForm.is_valid():
                    formData = columnForm.cleaned_data
                    columnModel = Column().initData(formData)
                    columnModel.save()
                    self.mc.flushNS2(self.params.COLUMN)
                    self.result = Result(True,'操作成功！','','/admin/list!Catalog.html',2)
                    self.viewData["etype"] = 'column'
                    self.viewData["e"] = columnModel
                    self.result.html = self.render("/admin/include/element.html")._container[0]
                else:
                    self.result = Result(False,columnForm.GetErrors(),'','/admin/list!Catalog.html',3)
        except Exception, ex:
            self.result = Result(False,str(ex),'','/admin/list!Catalog.html',3)
        finally:
            return self.render_json()
        
    def edit(self, **args):
        columnForm = ColumnForm()
        if self.request.POST:
            columnForm = ColumnForm(self)
            if columnForm.is_valid():
                formData = columnForm.cleaned_data
                objId = int(self.request.POST['_id'])
                if objId > -1:
                    columnModel = Column().initData(formData)
                    columnModel.id = objId
                    columnModel.save()
                    self.mc.flushNS2(self.params.COLUMN)
                    self.result = Result(True,'操作成功！','','/admin/list!Catalog.html',2)
                else:
                    self.result = Result(False,columnForm.GetErrors(),'','/admin/list!Column.html',3)
            else:
                self.result = Result(False,columnForm.GetErrors(),'','/admin/list!Column.html',3)
        if self.request.POST.has_key('_retType') and self.request.POST['_retType'] == 'json':
            return self.render_json()
        else:
            self.viewData["result"] = self.result
            return self.render('/include/skip.html')
            
    def delete(self, **args):
        try:
            if self.request.POST:
                if Catalog.objects.filter(column=int(args['id'])).count() > 0:
                    self.result.flag = False
                    self.result.msg = "当前栏目下存在目录，不允许删除！"
                else:
                    columnModel = Column.objects.get(id=int(args['id']))
                    columnModel.delete()
                    self.mc.flushNS2(self.params.COLUMN)
                    self.result.flag = True
                    self.result.msg = "删除成功！"
        except Exception,ex:
            self.result.flag = False
            self.result.msg = str(ex)
        if self.request.POST.has_key('_retType') and self.request.POST['_retType'] == 'json':
            return self.render_json()
            
    def listView(self, **args):
        columnList, columnModels = [], list(Column.objects.all().order_by('orderby'))
        for column in columnModels:
            columnList.append(ColumnForm(self,modelData=column))
        columnForm = ColumnForm()
        self.viewData['columnList'] = columnList
        self.viewData['columnForm'] = columnForm
        return self.render('/admin/listColumn.html')
    
    def detailView(self, **args):
        column = Column.objects.get(id=int(args['id']))
        columnForm = ColumnForm(self,modelData=column)
        self.viewData["etype"] = 'detail'
        self.viewData["e"] = columnForm
        return self.render('/admin/include/detail.html')   
    
def action(request, **args):
    return apply(ColumnView(request).action, (), args)